#  -*-  coding: utf-8 -*-

import re
import requests
from datetime import datetime
from bs4 import BeautifulSoup


class WeatherInfo():
    def __init__(self):
        pass

    def getWeather(self):
        print('WeatherInfo, getWeather...')
        headers = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit'
                              '/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safar'
                              'i/537.36',
            }

        res2 = requests.get('http://tianqi.moji.com/', headers=headers)

        soup = BeautifulSoup(res2.text, "html.parser")
        temperature = soup.find('div', attrs={'class': 'wea_weather clearfix'}).em.getText()
        #print('temperature=%s' % temperature)

        weather = soup.find('div', attrs={'class': 'wea_weather clearfix'}).b.getText()
        #print('weather=%s' % weather)

        sd = soup.find('div', attrs={'class': 'wea_about clearfix'}).span.getText()
        #print('sd=%s' % sd)

        sd_num = re.search(r'\d+', sd).group()
        #sd = sd.replace(sd_num, sd_num_zh)
        wind = soup.find('div', attrs={'class': 'wea_about clearfix'}).em.getText()
        #print('wind=%s' % wind)

        aqi = soup.find('div', attrs={'class': 'wea_alert clearfix'}).em.getText()
        #print('aqi=%s' % aqi)
        aqi_num = re.search(r'\d+', aqi).group()
        #aqi = aqi.replace(aqi_num, aqi_num_zh)

        info = soup.find('div', attrs={'class': 'wea_tips clearfix'}).em.getText()
        #print('info=%s' % info)

        #sd = sd.replace(' ', u'百分之').replace('%', '')
        aqi = u'' + aqi

        today = datetime.now().date()
        today_str = u'%s月%s日' % (today.month, today.day)
        #print('today_str=%s' % today_str)

        #text = u'早上好！今天是%s,天气%s,温度%s摄氏度,%s,%s,%s,%s' % \
        #       (today, weather, temp, sd, wind, aqi, info)


        weather_text = u'早上好！今天是%s,天气%s,温度%s摄氏度,%s,%s,空气质量%s,%s' % \
               (today_str, weather, temperature, sd, wind, aqi, info)

        #print('Weather=%s' % weather_text)
        print('WeatherInfo, getWeather finished.')
        return weather_text


if __name__ == '__main__':
    weather = WeatherInfo()
    weather_text = weather.getWeather()
    print('Weather=%s' % weather_text)
